package io.renren.modules.app.controller;


import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.form.PasswordForm;
import io.renren.modules.app.form.RegisterForm;
import io.renren.modules.app.service.UserService;
import io.renren.modules.quickService.constants.UserExtraEnum;
import io.renren.modules.quickService.entity.UserEntity;
import io.renren.modules.quickService.entity.UserExtraEntity;
import io.renren.modules.quickService.service.AreacodeService;
import io.renren.modules.quickService.service.UserExtraService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 用户注册
 *
 * @author zenger380@163.com
 * @since 3.1.0 2018-09-25
 */
@RestController
@RequestMapping("/app/account")
@Api(value = "APP用户账号相关", tags = {"APP用户账号相关接口"})
public class AppRegisterController {
    @Autowired
    private UserService appUserService;
    @Autowired
    private UserExtraService userExtraService;
    @Autowired
    private AreacodeService areacodeService;

    @PostMapping("register")
    @ApiOperation("注册")
    public R register(@RequestBody RegisterForm form) {
        ValidatorUtils.validateEntity(form);
        UserEntity user = appUserService.queryByMobile(form.getMobile());
        if (user != null) {
            return R.error("该手机号已被使用");
        }
        UserExtraEntity extra = userExtraService.queryUserExtraByID(form.getId());
        if (extra != null) {
            return R.error("该身份证号已被使用");
        }
        String address = areacodeService.getAddressByLastAdminCode(form.getAdminCode());
        user = new UserEntity();
        user.setMobile(form.getMobile());
        user.setUsername(form.getMobile());
        user.setPassword(DigestUtils.sha256Hex(form.getPassword()));
        user.setCreateTime(new Date());
        user.setAddress(address);
        user.setAdminCode(form.getAdminCode());
        appUserService.insert(user);
        user = appUserService.queryByMobile(user.getMobile());
        UserExtraEntity userExtra = new UserExtraEntity();
        userExtra.setUserId(user.getUserId());
        userExtra.setExtraName(form.getExtraName());
        userExtra.setMale(form.getMale());
        userExtra.setId(form.getId());
        userExtra.setIdImageA(form.getIdImageA());
        userExtra.setIdImageB(form.getIdImageB());
        userExtra.setMobile(user.getMobile());
        userExtra.setWechatId(form.getWechatId());
        userExtra.setBank(form.getBank());
        userExtra.setCardNum(form.getCardNum());
        userExtra.setAuditStatus(UserExtraEnum.TO_AUDIT.getKey());
        userExtra.setAdminCode(form.getAdminCode());
        userExtra.setCreateTime(new Date());
        userExtra.setUpdateTime(new Date());
        userExtra.setAddress(address);
        userExtraService.insert(userExtra);
        return R.ok();
    }

    @Login
    @PostMapping("/password")
    @ApiOperation("修改密码")
    public R password(@RequestBody PasswordForm form, HttpServletRequest request) {
        Assert.isBlank(form.getNewPassword(), "新密码不为能空");
        //sha256加密
        String password = DigestUtils.sha256Hex(form.getPassword());
        //sha256加密
        String newPassword = DigestUtils.sha256Hex(form.getNewPassword());
        //更新密码
        boolean flag = appUserService.updatePassword((Long) request.getAttribute("userId"), password, newPassword);
        if (!flag) {
            return R.error("原密码不正确");
        }
        return R.ok();
    }
}
